﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HikVisionTool.Helper
{
    public class HttpHelper
    {
        public static string HttpPostData(string url, string jsonData)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            request.KeepAlive = true;
            string param = string.Format("json={0}", jsonData);
            byte[] bs = Encoding.UTF8.GetBytes(param);
            try
            {
                using (Stream streamWriter = request.GetRequestStream())
                {
                    streamWriter.Write(bs, 0, bs.Length);
                    streamWriter.Close();
                }
                HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
                StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                string responseContent = streamReader.ReadToEnd();
                httpWebResponse.Close();
                streamReader.Close();
                return responseContent;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static bool PostJsonData(string url, string jsonData)
        {
            string responseContent = "";
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "POST";
                request.ContentType = "application/json;charset=UTF-8";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                request.KeepAlive = true;

                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(jsonData);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
                HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
                StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                responseContent = streamReader.ReadToEnd();
                httpWebResponse.Close();
                streamReader.Close();
                if (! responseContent.Contains("true"))
                {
                    //FileHelper.WriteLog(jsonData);
                    //FileHelper.WriteLog(responseContent);
                    return false;
                }
                return true;
                //return responseContent;
            }

            catch (Exception ex)
            {
                //FileHelper.WriteLog(ex.ToString());
                return false;
            }

        }
    }
}
